package com.ourteam.product.model.dao.impl;

import com.ourteam.product.model.dao.IProductModelBaseDAO;
import com.ourteam.product.model.dao.ProductModelBase;
import com.ourteam.product.model.dao.ProductModelBaseDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 IRKT 2017
 * Auto Generate Date : 2017-11-17 11:01:21
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractProductModelBaseDAOJdbcImpl extends AbstractSpringDAO
    implements IProductModelBaseDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, ModelBaseName, ModelBaseType, CreatorName, CreateTime,
            LastModifierName, LastModifyTime, Status, Remarks, OwnerId
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.ModelBaseName,
            _DataObjectProperty.ModelBaseType, _DataObjectProperty.CreatorName,
            _DataObjectProperty.CreateTime, _DataObjectProperty.LastModifierName,
            _DataObjectProperty.LastModifyTime, _DataObjectProperty.Status,
            _DataObjectProperty.Remarks, _DataObjectProperty.OwnerId
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<ProductModelBase, ProductModelBaseDAOQueryBean> daoHelper;
    private com.ourteam.product.model.dao.impl.AbstractProductModelBaseRelateDAOJdbcImpl ProductModelBaseRelateDao;

    public AbstractProductModelBaseDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<ProductModelBase, ProductModelBaseDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aProductModelBase
         * @throws Exception
         */
    public boolean checkIsUnique(ProductModelBase aProductModelBase)
        throws Exception {
        if (aProductModelBase == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aProductModelBase.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aProductModelBase
     * @throws Exception
     */
    public void insert(ProductModelBase aProductModelBase)
        throws Exception {
        super.doInsert(aProductModelBase);
    }

    /**
         * Batch Insert Datas
         * @param aProductModelBase
         * @throws Exception
         */
    public void batchInsert(ProductModelBase[] aProductModelBases)
        throws Exception {
        super.doBatchInsert(aProductModelBases);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(ProductModelBaseDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(ProductModelBase aProductModelBase)
        throws Exception {
        delete(aProductModelBase.getId());
    }

    /**
     * Update Data Selective
     * @param aProductModelBase
     * @throws Exception
     */
    public void update(ProductModelBase aProductModelBase)
        throws Exception {
        if (aProductModelBase.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aProductModelBase);

        ProductModelBase tempObj = findById(aProductModelBase.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aProductModelBase, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aProductModelBases[]
     * @throws Exception
     */
    public void batchUpdate(ProductModelBase[] aProductModelBases)
        throws Exception {
        super.doBatchUpdate(aProductModelBases);
    }

    /**
         * Update Data By Condictions
         * @param aProductModelBase
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(ProductModelBase aProductModelBase,
        ProductModelBaseDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aProductModelBase, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(ProductModelBaseDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(ProductModelBase aProductModelBase)
        throws Exception {
        if (aProductModelBase.getLastModifierName() == null) {
            throw new Exception(
                "The ProductModelBase.lastModifierName value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public ProductModelBase findById(final long id) throws Exception {
        ProductModelBaseDAOQueryBean queryBean = new ProductModelBaseDAOQueryBean();

        ProductModelBaseDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        ProductModelBase[] ProductModelBases = query(queryBean);

        if (ProductModelBases.length == 0) {
            return null;
        } else {
            return ProductModelBases[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return ProductModelBase[]
         * @throws Exception
         */
    public ProductModelBase[] query(ProductModelBaseDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcProductModelBaseRowMapper());

        return (ProductModelBase[]) result.toArray(new ProductModelBase[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        ProductModelBaseDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(ProductModelBaseDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(ProductModelBase[] aProductModelBases,
        ProductModelBaseDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aProductModelBases, queryBean);
    }

    public void synchronize(ProductModelBase[] aProductModelBases,
        ProductModelBaseDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<ProductModelBase> callBack)
        throws Exception {
        daoHelper.synchronize(aProductModelBases, queryBean, callBack);
    }

    public void setProductModelBaseRelateDao(
        com.ourteam.product.model.dao.IProductModelBaseRelateDAO ProductModelBaseRelateDao) {
        this.ProductModelBaseRelateDao = (com.ourteam.product.model.dao.impl.AbstractProductModelBaseRelateDAOJdbcImpl) ProductModelBaseRelateDao;
    }

    /**
     * Query Related ProductModelBaseRelate
    * @param queryBean
     * @throws Exception
     */
    public com.ourteam.product.model.dao.ProductModelBaseRelate[] queryProductModelBaseRelate(
        ProductModelBaseDAOQueryBean queryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.product.model.dao.IProductModelBaseRelateDAO.ModelBaseId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.product.model.dao.impl.ProductModelBaseRelateDAO.EntityName +
            " RELATED_1RORL8_ProductModelBaseRelate";

        String[] entityFields = com.ourteam.product.model.dao.impl.ProductModelBaseRelateDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.product.model.dao.impl.ProductModelBaseRelateDAO.ENTITY_PROPERTIES;

        List list = ProductModelBaseRelateDao.doQuery(entityName, entityFields,
                entityProperties, queryBean, joinSQL.toString(),
                new com.ourteam.product.model.dao.impl.ProductModelBaseRelateDAO.JdbcProductModelBaseRelateRowMapper());

        return (com.ourteam.product.model.dao.ProductModelBaseRelate[]) list.toArray(new com.ourteam.product.model.dao.ProductModelBaseRelate[list.size()]);
    }

    /**
     * Query Related ProductModelBaseRelate Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] queryProductModelBaseRelateSelective(
        ProductModelBaseDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelBaseRelateDAO.Id,
                "id");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelBaseRelateDAO.ModelBaseId,
                "modelBaseId");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelBaseRelateDAO.ModelDomainId,
                "modelDomainId");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelBaseRelateDAO.Status,
                "status");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelBaseRelateDAO.Remarks,
                "remarks");
        }

        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.product.model.dao.IProductModelBaseRelateDAO.ModelBaseId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.product.model.dao.impl.ProductModelBaseRelateDAO.EntityName +
            " RELATED_1RORL8_ProductModelBaseRelate";

        return ProductModelBaseRelateDao.doQuerySelective(entityName,
            queryBean, joinSQL.toString());
    }

    /**
     * Update Related ProductModelBaseRelate
     * @param aProductModelBaseRelate
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void updateProductModelBaseRelate(
        com.ourteam.product.model.dao.ProductModelBaseRelate aProductModelBaseRelate,
        ProductModelBaseDAOQueryBean aQueryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.product.model.dao.IProductModelBaseRelateDAO.ModelBaseId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.product.model.dao.impl.ProductModelBaseRelateDAO.EntityName +
            " RELATED_1RORL8_ProductModelBaseRelate";

        String[] entityFields = com.ourteam.product.model.dao.impl.ProductModelBaseRelateDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.product.model.dao.impl.ProductModelBaseRelateDAO.ENTITY_PROPERTIES;

        ProductModelBaseRelateDao.doUpdateByQuery(entityName, entityFields,
            entityProperties, joinSQL.toString(), aProductModelBaseRelate,
            aQueryBean);
    }

    /**
     * Delete Related ProductModelBaseRelate
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void deleteProductModelBaseRelate(
        ProductModelBaseDAOQueryBean aQueryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.product.model.dao.IProductModelBaseRelateDAO.ModelBaseId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.product.model.dao.impl.ProductModelBaseRelateDAO.EntityName; // + " RELATED_1RORL8_ProductModelBaseRelate";

        ProductModelBaseRelateDao.doDeleteByQuery(entityName,
            joinSQL.toString(), aQueryBean);
    }

    public static class JdbcProductModelBaseRowMapper implements RowMapper<ProductModelBase> {
        public ProductModelBase mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            ProductModelBase object = new ProductModelBase();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object modelBaseName = null;

                modelBaseName = resultSet.getObject(columnIndex++);

                object.setModelBaseNameNull(resultSet.getBoolean(columnIndex++));

                if (modelBaseName != null) {
                    setBeanValue(object, _DataObjectProperty.ModelBaseName,
                        modelBaseName);
                }

                Object modelBaseType = null;

                modelBaseType = resultSet.getObject(columnIndex++);

                object.setModelBaseTypeNull(resultSet.getBoolean(columnIndex++));

                if (modelBaseType != null) {
                    setBeanValue(object, _DataObjectProperty.ModelBaseType,
                        modelBaseType);
                }

                Object creatorName = null;

                creatorName = resultSet.getObject(columnIndex++);

                object.setCreatorNameNull(resultSet.getBoolean(columnIndex++));

                if (creatorName != null) {
                    setBeanValue(object, _DataObjectProperty.CreatorName,
                        creatorName);
                }

                Object createTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    createTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    createTime = resultSet.getObject(columnIndex++);
                }

                object.setCreateTimeNull(resultSet.getBoolean(columnIndex++));

                if (createTime != null) {
                    setBeanValue(object, _DataObjectProperty.CreateTime,
                        createTime);
                }

                Object lastModifierName = null;

                lastModifierName = resultSet.getObject(columnIndex++);

                object.setLastModifierNameNull(resultSet.getBoolean(
                        columnIndex++));

                if (lastModifierName != null) {
                    setBeanValue(object, _DataObjectProperty.LastModifierName,
                        lastModifierName);
                }

                Object lastModifyTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    lastModifyTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    lastModifyTime = resultSet.getObject(columnIndex++);
                }

                object.setLastModifyTimeNull(resultSet.getBoolean(columnIndex++));

                if (lastModifyTime != null) {
                    setBeanValue(object, _DataObjectProperty.LastModifyTime,
                        lastModifyTime);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object ownerId = null;

                ownerId = resultSet.getObject(columnIndex++);

                object.setOwnerIdNull(resultSet.getBoolean(columnIndex++));

                if (ownerId != null) {
                    setBeanValue(object, _DataObjectProperty.OwnerId, ownerId);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
